package com.gin.kotlin.gf2gamecommunity.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedLiveData
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedPagingData
import com.gin.kotlin.gf2gamecommunity.callback.AbstractNothingCallback
import com.gin.kotlin.gf2gamecommunity.pagingsource.UserFansPagingSource
import com.gin.kotlin.gf2gamecommunity.pagingsource.UserFollowsPagingSource
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.other.UserRecommend
import com.gin.kotlin.response.user.UserInfoSimple

/**
 * 关注相关ViewModel
 * @property userRecommend 关注推荐
 * @property userFollows 用户关注
 * @property userFans 用户粉丝
 * @constructor
 */
class UserFollowViewModel : ViewModel() {

    val userRecommend: CachedLiveData<Res<List<UserRecommend>>, List<UserRecommend>> = CachedLiveData(
        requestMethod = { App.INSTANCE.api.otherApi.getUserRecommend() },
        bodyTransform = { it.data }
    )

    val userFollows = CachedPagingData<UserInfoSimple, Int>(scope = viewModelScope) { UserFollowsPagingSource(it) }

    val userFans = CachedPagingData<UserInfoSimple, Int>(scope = viewModelScope) { UserFansPagingSource(it) }


    /**
     * 关注/取关用户
     * @param userId Int
     * @param onSuccess (message: String) -> Unit
     */
    fun follow(userId: Int, onSuccess: (message: String) -> Unit) {
        userApi().followUser(userId).enqueue(object : AbstractNothingCallback() {
            override fun onSuccess(message: String) {
                onSuccess(message)
            }
        })
    }

    private fun userApi() = App.INSTANCE.api.userApi

    companion object {
        private val TAG = UserFollowViewModel::class.java.simpleName
    }
}